package com.scs.application.modules.upms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageRequest;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.design.service.QueryGenerator;
import com.scs.application.modules.upms.entity.Role;
import com.scs.application.modules.upms.entity.User;
import com.scs.application.modules.upms.mapper.RoleMapper;
import com.scs.application.modules.upms.request.RoleQueryRequest;
import com.scs.application.modules.upms.service.RoleService;
import com.scs.application.modules.upms.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 */
@AllArgsConstructor
@RestController
@RequestMapping("/upms/roles")
public class RoleController extends CrudController<Role, String, RoleService> implements ImAndExAbleController<RoleQueryRequest> {

    private final UserService userService;
    private final RoleMapper roleMapper;

    @GetMapping("/page")
    public PageResult page(RoleQueryRequest request) {
        IPage page = service.page(request);

        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(RoleQueryRequest request) {
        List<Role> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/{roleId}/users/page")
    public PageResult roleUser(@PathVariable("roleId") String roleId, PageRequest pageRequest) {
        IPage<User> page = service.pageUsers(roleId, pageRequest);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    /**
     * 删除指定角色下指定用户
     *
     * @param roleId
     * @param userId
     * @return
     */
    @DeleteMapping("/{roleId}/users/{userId}")
    public CommonResult deleteRoleUser(@PathVariable("roleId") String roleId, @PathVariable("userId") String userId) {
        boolean success = userService.unassignRole(userId, roleId);
        return success ? CommonResult.ok() : CommonResult.error();
    }

    /**
     * 为指定角色添加多个用户
     *
     * @param roleId
     * @param userIds
     * @return
     */
    @PostMapping("/{roleId}/users")
    public CommonResult assignRole(@PathVariable("roleId") String roleId, @RequestBody  String[] userIds) {
        if (userIds == null || userIds.length == 0) {
            return CommonResult.error();
        }
        boolean success = userService.assignRole(Lists.newArrayList(userIds), roleId);
        return success ? CommonResult.ok() : CommonResult.error();
    }

    /***
     * @param request
     * @Description:  通用分页
     */
    @GetMapping("/pageUserByRole")
    public PageResult pageUserByRole(HttpServletRequest request,@RequestParam Map<String, String> reqData) {
        IPage<User> page = QueryGenerator.parsePage(request);
        QueryWrapper<User> queryWrapper = QueryGenerator.initQueryWrapper(request);
        String roleId = "";
        if (reqData.containsKey("userParam")) {
            JSONObject ob = JSON.parseObject(reqData.get("userParam") + "");
            roleId = ob.getString("roleId");
        }
        BusinessException.throwErrorIf(StringUtils.isBlank(roleId),"缺少参数roleId");
        IPage pageResult = roleMapper.pageUserByRole(page, queryWrapper,roleId);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
